using Crawler.Domain;
using Crawler.Domain.Sources;
using Crawler.DomainServices.Processors;
using log4net;

namespace Crawler.ContentProcessors
{
	internal abstract class PageContentProcessor< TTarget> : IPageContentProcessor
	{
		private static readonly ILog Logger = LogManager.GetLogger("AppLogger");

		public abstract PageSource ForSource { get; }


		public void ProcessPageContent<TSource>(TSource src) where TSource : ProcessableSource
		{
			if (!MustProcess(src.AbsoluteUri))
			{
				Logger.InfoFormat("THIS WONT BE PARSED {0}", src.AbsoluteUri);
				return;
			}

			//crear tipo normalizado de dominio
			TTarget file = Parse(src);
			if (file == null)
			{
				Logger.ErrorFormat("PageContentProcessor.ProcessPageContent  - SessionId:{0} NullParsed:{1}", src.CrawlSessionId,
					src.AbsoluteUri);
				return;
			}
			Record(file);
		}

		protected abstract void Record(TTarget data);


		protected abstract TTarget Parse<TSource>(TSource src) where TSource : ProcessableSource;

		protected abstract bool MustProcess(string src);
	}
}